package controllers

import (
	"database/sql"
	"net/http"
	"strconv"

	"web-service/models"

	"github.com/gin-gonic/gin"
)

type TagController struct {
	DB *sql.DB
}

// GetTagList 获取标签列表
func (tc *TagController) GetTagList(c *gin.Context) {
	// 获取分页参数
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))

	// 获取标签列表
	tags, total, err := models.GetTagList(tc.DB, page, pageSize)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "获取标签列表失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": gin.H{
			"list":  tags,
			"total": total,
		},
	})
}

// CreateTag 创建新标签
func (tc *TagController) CreateTag(c *gin.Context) {
	// 获取请求参数
	var req struct {
		Name string `json:"name" binding:"required"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "请求参数错误"})
		return
	}

	// 创建标签
	err := models.CreateTag(tc.DB, req.Name)
	if err != nil {
		if _, ok := err.(models.ErrRecordExists); ok {
			c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
			return
		}
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "创建标签失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"code": 200, "message": "创建成功"})
}

// UpdateTag 更新标签信息
func (tc *TagController) UpdateTag(c *gin.Context) {
	// 获取标签ID
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的标签ID"})
		return
	}

	// 获取请求参数
	var req struct {
		Name string `json:"name" binding:"required"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "请求参数错误"})
		return
	}

	// 更新标签
	err = models.UpdateTag(tc.DB, id, req.Name)
	if err != nil {
		if _, ok := err.(models.ErrRecordExists); ok {
			c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
			return
		}
		if err == sql.ErrNoRows {
			c.JSON(http.StatusNotFound, gin.H{"code": 404, "message": "标签不存在"})
			return
		}
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "更新标签失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"code": 200, "message": "更新成功"})
}

// GetAllTags 获取所有标签列表
func (tc *TagController) GetAllTags(c *gin.Context) {
	// 获取所有标签列表
	tags, err := models.GetAllTags(tc.DB)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "获取标签列表失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": tags,
	})
}

// DeleteTag 删除标签
func (tc *TagController) DeleteTag(c *gin.Context) {
	// 获取标签ID
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的标签ID"})
		return
	}

	// 删除标签
	err = models.DeleteTag(tc.DB, id)
	if err != nil {
		if err == sql.ErrNoRows {
			c.JSON(http.StatusNotFound, gin.H{"code": 404, "message": "标签不存在"})
			return
		}
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "删除标签失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"code": 200, "message": "删除成功"})
}
